// pages/play/record.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    size: 20,
    isBottom: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '正在努里加载中',
    })
    app.request(app.apiDomain + 'index.php?c=step&a=getlist', {
      page: that.data.page,
      size: that.data.size
    }, function(res) {
      if (res.data.status == 1) {
        let data = res.data.data
        for (let item of data) {
          let time = item.end_time - item.start_time
          console.log(time)
          let km = parseFloat(item.end_step * 0.6 / 1000).toFixed(3)
          let mi = item.end_step * 0.6
          km = km.substring(0, km.length - 1)
          let speed = parseFloat(mi / time).toFixed(2).toString()
          let speed_h = speed.substring(0, speed.indexOf('.'))
          let speed_m = speed.substring(speed.indexOf('.') + 1)
          let hour = parseInt(time / 60 / 60 % 24)
          let min = parseInt(time / 60 % 60) < 10 ? '0' + parseInt(time / 60 % 60) : parseInt(time / 60 % 60)
          let sec = parseInt(time % 60) < 10 ? '0' + parseInt(time % 60) : parseInt(time % 60)
          item['speed_h'] = speed_h
          item['speed_m'] = speed_m
          item['hour'] = hour
          item['min'] = min
          item['sec'] = sec
          item['km'] = km
          item['mileage'] = parseInt(item.mileage)
        }
        that.setData({
          list: data
        })
        wx.hideLoading()
      }
    })
  },
  scroll_get: function () {
    let that = this
    if (that.data.isBottom) {
      return false
    }
    that.data.page++
    app.request(app.apiDomain + 'index.php?c=step&a=getlist', {
      page: that.data.page,
      size: that.data.size
    }, function (res) {
      if (res.data.data.length == 0) {
        wx.showToast({
          title: '没有更多了~',
          icon: 'none',
          duration: 2000
        })
        that.setData({
          // page: this.data.page + 1,
          isBottom: true
        })
      } else if (res.data.data.length < that.data.size) {
        wx.showToast({
          title: '没有更多了~',
          icon: 'none',
          duration: 2000
        })
        that.setData({
          // page: this.data.page + 1,
          isBottom: true
        })
      } else {
        that.setData({
          list: that.data.list.concat(res.data.data),
          isBottom: false
        })
      }
    })
  },
  gotoResult: function (e) {
    let id = e.target.dataset.id
    console.log(e.target)
    wx.navigateTo({
      url: './result?order_id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})